package Servlets;

import main.ServerGlobals;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import protocol.HTTPRequest;
import protocol.HTTPResponse;
import protocol.Headers;
import SQL.SQLLiteHandler;

public class List extends Servlet {

	private HTTPResponse getUsers() {
		JSONArray res = fSQLLiteHandler.Query("select "
				+ WhatsAppConstatnts.USERNAME + "," + WhatsAppConstatnts.PHONE
				+ " from Users");
		String users = new String();

		for (int i = 0; i < res.size(); i++) {

			users += ((JSONObject) res.get(i)).get(WhatsAppConstatnts.USERNAME)
					.toString()
					+ "@"
					+ ((JSONObject) res.get(i)).get(WhatsAppConstatnts.PHONE)
							.toString() + "\n";
		}

		return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
				new JSONObject(), users.substring(0, users.length() - 1), false);
	}

	private HTTPResponse getGroups() {
		JSONArray res = fSQLLiteHandler.Query("select * from "
				+ WhatsAppConstatnts.TABLE_GROUPS + " order by "
				+ WhatsAppConstatnts.GROUP_NAME + " Desc");

		String CurrGroupName = null;
		if (res.size() > 0) {
			CurrGroupName = ((JSONObject) res.get(0)).get(
					WhatsAppConstatnts.GROUP_NAME).toString();
		}
		String result = new String(CurrGroupName + ": ");
		for (int i = 0; i < res.size(); i++) {

			if (CurrGroupName.equals(((JSONObject) res.get(i)).get(
					WhatsAppConstatnts.GROUP_NAME).toString())) {
				result += ((JSONObject) res.get(i)).get(
						WhatsAppConstatnts.PHONE).toString()
						+ ",";
			} else {
				result = result.substring(0, result.length() - 1);
				result += "\n";
				CurrGroupName = ((JSONObject) res.get(i)).get(
						WhatsAppConstatnts.GROUP_NAME).toString();
				result += CurrGroupName
						+ ": "
						+ ((JSONObject) res.get(i)).get(
								WhatsAppConstatnts.PHONE).toString() + ",";
			}
		}
		System.out.println(result.toString());
		return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
				new JSONObject(), result.substring(0, result.length() - 2),
				false);
	}

	private HTTPResponse getGroup(String Group) {
		JSONArray res = fSQLLiteHandler.Query("select * from "
				+ WhatsAppConstatnts.TABLE_GROUPS + " WHERE "
				+ WhatsAppConstatnts.GROUP_NAME + "='"
				+ SQLLiteHandler.clean(Group) + "'");
		String result = new String();
		for (int i = 0; i < res.size(); i++) {
			result += ((JSONObject) res.get(i))
					.get(WhatsAppConstatnts.USERNAME).toString() + ",";
		}
		return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
				new JSONObject(), result.substring(0, result.length() - 1),
				false);
	}

	protected boolean isError777(HTTPRequest httpRequest,
			ServerGlobals serverGlobals) {
		JSONObject msg = httpRequest.getfHttpmsg().toJSONObject();
		JSONArray res = fSQLLiteHandler.Query("select * from "
				+ WhatsAppConstatnts.TABLE_GROUPS + " where "
				+ WhatsAppConstatnts.GROUP_NAME + "='"
				+ SQLLiteHandler.clean(msg.get("List").toString()) + "'");
		if (res.size() == 0) {
			return true;
		}
		return false;
	}

	@Override
	public HTTPResponse process(HTTPRequest httpRequest,
			ServerGlobals serverGlobals) {
		if (!this.isCorrectMethod(httpRequest, Headers.POST)) {
			return new HTTPResponse(Headers.STATUS_405, Headers.HTTP_VERSION,
					new JSONObject(), "", false);
		}
		if (!this.isAuthorized(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_403, Headers.HTTP_VERSION,
					new JSONObject(), "", false);
		}
		if (!this.areParametersRequiredSent(new String[] { "List" },
				httpRequest)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_273, false);
		}

		JSONObject msg = httpRequest.getfHttpmsg().toJSONObject();

		if (msg.get("List").toString().equals("Users")) {
			return getUsers();
		}

		if (msg.get("List").toString().equals("Groups")) {
			return getGroups();
		}

		if (this.isError777(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_777, false);
		}

		return getGroup(msg.get("List").toString());

	}
}
